<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        组织架构
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>两个负责人</el-col>
              <!-- 下拉菜单 -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel('')"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="isShowDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <!-- 子组件 -->
        <deptDialog
          v-if="isShowDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'

import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 表单校验需要的数据
      originList: [],
      // 控制弹框xianshiyincang
      isShowDialog: false,
      // 声明id
      curId: '',
      // 使用变量判断是否为编辑
      isEdit: false
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const { data } = await getDepartments()
      console.log(data)
      data.depts.shift()
      // 绑定表单需要的数据
      this.originList = data.depts.map(item => {
        return {
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid
        }
      })
      // 绑定数据
      this.list = tranListToTreeData(data.depts)
    },
    // 添加部门
    hAdd(id) {
      // 点击添加子部门显示弹框
      this.isShowDialog = true
      // 更新id
      this.curId = id
    },
    hSuccess() {
      // 关闭弹框
      this.isShowDialog = false
      // 更新数据
      this.loadDepartments()
    },
    // 删除部门
    async hDel(id) {
      // 弹层询问，是否退出
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delDepartment(id)
          this.$message.success('删除成功')
          this.loadDepartments()
        })
        .catch(() => {
          // 用户取消退出
        })
    },
    // 编辑部门
    hEdit(id) {
      // 将isEdit设置为true
      this.isEdit = true
      // 也要显示弹框
      this.isShowDialog = true
      // 也要更新id
      this.curId = id
    },
    // 关闭弹框
    hClose() {
      this.isShowDialog = false
    }
  }
}
</script>
